<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr"><head>
  <title>CS-Script and WebServices</title><meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <xml></xml>
  <link rel="stylesheet" type="text/css" href="MSDN.css"></head>
<body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
  <tbody>
    <tr id="hdr">
      <td class="runninghead">
      <p align="left">CS-Script 3.27.0</p>
      </td>
      <td class="product"> </td>
    </tr>
  </tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">CS-Script and Web Services</h1>
</div>
</div>
<div id="nstext">
<p style="text-align: left;">Web Services are web-based enterprise
applications that use open, XML-based standards
and transport protocols to exchange data with calling clients.&nbsp;Web
Services are becoming more and more popular among developers as a
choice of the technology, which offers high level of interoperability.
The simplicity of creating and consuming Web Services in .NET also
contributes to their popularity.<br>
 </p>
<p style="font-weight: bold; font-style: italic;">Consuming Web Services</p>
<p style="text-align: left;">Traditionally, in order to consume Web Service from managed application,&nbsp;you would need to execute <span style="font-style: italic;">WSDL.exe </span>to
generate .cs file containing the C# implementation of the Web Service
proxy class. Such&nbsp;class can be utilised after it is&nbsp;compiled
into either&nbsp;class library or executable.&nbsp;</p>
<p>CS-Script offers even simpler way of accessing the Web Services: "<span style="font-style: italic;">Single-line Web Service access</span>". It means that proxy class can be made available with a single line of code in the C# script file.&nbsp;</p>
<p style="text-align: left;">It is possible because you can automate&nbsp;generating&nbsp;the proxy
class with&nbsp;<a href="pre_post_scripts.html">pre-script directive</a>. Such directive would invoke the&nbsp;<span style="font-style: italic;">wsdl.cs</span> script (<a href="Script_library.html">Script Library</a>), which is capable of generating Web Service proxy classes.&nbsp;</p>
<p style="text-align: left;">Utilising the generated&nbsp;proxy class in&nbsp;the CS-Script is extremely simple.
It is because CS-Script can use external modules in a form of C# code.
In other words the file containing the proxy class can be just <a href="Importing_scripts.html">imported&nbsp;at execution time</a>, what eliminates need for compiling it into assembly (see <span style="font-style: italic;">googleWebService.cs</span> sample (of the&nbsp;<a href="Script_library.html">Script Library</a>) for details).</p>
<span style="font-weight: bold;"></span>This is the example of the script, which uses&nbsp;<span style="color: rgb(51, 0, 153);">SayHello</span> operation of the&nbsp;<span style="color: rgb(51, 0, 153);">HelloWorld</span> Web Service.&nbsp;
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_prescript&nbsp;wsdl(http://localhost/hello/hello.asmx?WSDL,&nbsp;HelloService);<br>
      </font><font color="#008000" size="2">//css_import&nbsp;HelloService;</font></font><font face="Courier New" size="2"><font color="#0000ff" size="2"><br>
using</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Xml;<br>
      <br>
      <font color="#0000ff" size="2">class</font>&nbsp;Program<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#0000ff" size="2">new</font>&nbsp;HelloWorld().SayHello());<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
<p><span style="font-weight: bold; font-style: italic;">Note</span><span style="font-style: italic;">: If you wish you can instruct wdsl.cs (by using parameters)&nbsp;to generate proxy class only if it does not exist.</span></p>
<p style="font-style: italic;">Always check if the Web Service is available with the IE (eg. in IE go to http://localhost/hello/hello.asmx?WSDL).</p>
<p style="font-style: italic;"></p>
<p><span style="font-style: italic;"></span>The location of the&nbsp;generated proxy class file depends on the HideAutoGeneratedFiles setting value (see <a href="Config.html">configuration console</a> for details). The location of this file does not affect the script code in any way.</p>
<br>
<span style="font-style: italic;">The Vsiual Studio project sample for hosting CS-Script from WebService can be found in Samples Library (Samples/WebService).</span>
<p>
</p>
<p><br>
</p>
<ul>
</ul>
<p style="font-weight: bold; font-style: italic;">Creating Web Services</p>
<p>Creating a simple Web Service in Visual Studio (VS) is trivial. It not
only builds&nbsp;the service it can also create the deployment
project for you. Despite the convenience of creating the Web Service
this
way sometimes it creates an impression of some kind of "magic",
happening behind the scene, which actually converts a VS project code into Web
Services.</p>
<p>Well, building and configuring the Web Service is much simpler than
many developers would imagine. &nbsp;In fact with CS-Script you can
easily convert an appropriate C# script into Web Service. And such
conversion is open for analysis and adjustments (if required) because
it is implemented as a script <span style="font-style: italic;">css2ws.cs </span>(<a href="Script_library.html">Script Library</a>).&nbsp;</p>
<p style="margin-left: 40px;"><span style="font-style: italic;">CS-Script
is not intended to be a replacement of the MS Visual Studio as a main
Web Service development tool. It just allows simple and efficient Web
Service testing/prototyping.</span></p>
<p>This is the example of the <span style="font-style: italic;">Hello.cs</span> script containing the implementation of the HelloWorld Web Service.&nbsp;</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Web;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;System.Web.Services;<br>
      <br>
      <font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">class</font>&nbsp;HelloWorld&nbsp;:&nbsp;System.Web.Services.WebService<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;[WebMethod]<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">string</font>&nbsp;SayHello()<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">return</font>&nbsp;<font color="#800000" size="2">"Hello&nbsp;World&nbsp;(non-VS&nbsp;WebService)"</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
The following command, if executed in&nbsp;command prompt, will build and configure HelloWorld Web Service:
				
			
<div style="margin-left: 40px;" class="syntax">
				&nbsp;cscs css2ws Hello.cs HelloWorld&nbsp;
			</div>
After the conversion you can test the Web Service by executing the code
sample from the "Consuming Web Services" section of this article. The
output should be as following:<br>
<p><img style="width: 493px; height: 224px;" alt="" src="Images/ws1.PNG"></p>
<p>Note that in order to hoste Web Services MS IIS must be installed. <br>
</p>
<br>
<h4 class="dtH4">See Also&nbsp;</h4>
<p><a href="pre_post_scripts.html"></a><a href="pre_post_scripts.html"><span style="text-decoration: underline;">Pre- and Post-execution scripts</span></a>&nbsp;|&nbsp;<a href="Importing_scripts.html">Importing scripts</a> | <a href="Script_library.html">Script Library</a></p>
</div>
<br>
</body></html>

